<style>
.{{$t|escape:'html'}}_image_button {
  cursor: pointer;
}
table.{{$t|escape:'html'}}_list {
  width: 1200px;
  border-left: 1px solid #dddddd;
  border-top: 1px solid #dddddd;
  margin-bottom: 1px;
}
table.{{$t|escape:'html'}}_list td.border {
  border-right: 1px solid #dddddd;
  border-bottom: 1px solid #dddddd;
}
table.{{$t|escape:'html'}}_list th {
  text-align: left;
  height: 35px;
  background-color: #eaf2f5;
  padding: 5px 0 5px 10px; 
  border-right: 1px solid #ffffff;
  border-bottom: 1px solid #ffffff;
  cursor: pointer;
}
table.{{$t|escape:'html'}}_list td.header {
  text-align: left;
  height: 35px;
  background-color: #eaf2f5;
  padding: 5px 0 5px 10px; 
  border-right: 1px solid #ffffff;
  border-bottom: 1px solid #ffffff;
}
table.{{$t|escape:'html'}}_list th:hover {
  background-color: #d6e3e8;
}
table.{{$t|escape:'html'}}_list tr.drop td {
  padding-left: 0;
  border-right: 0;
  border-bottom: 0;
}
table.{{$t|escape:'html'}}_list tr.rowodd td {
  padding-left: 10px;
  height: 35px;
  background-color: #eeeeee;
  border-right: 1px solid #dddddd;
  border-bottom: 1px solid #dddddd;
}
table.{{$t|escape:'html'}}_list tr.roweven td {
  padding-left: 10px;
  height: 35px;
  border-right: 1px solid #dddddd;
  border-bottom: 1px solid #dddddd;
}
</style>

{{include file='admin/ecommerce/searchbox.tpl'}}
<table id="{{$t|escape:'html'}}_parcels" class="{{$t|escape:'html'}}_list" cellPadding="0" cellSpacing="0">
  <tr>
    <td style="height: 35px;padding-left: 10px;"><input id="{{$t|escape:'html'}}_parcels_select" type="checkbox"></input></td>
    <td class="border" style="padding-left: 10px;border-bottom: 0;" colSpan=7>
      <table>
        <tr>
          <td><select id="{{$t|escape:'html'}}_parcels_actions"><option>{{t escape='html'}}Actions...{{/t}}</option></select></td>
          <td>
            <span class="image_button" style="width: 140px;" onclick="location.href='admin.php?path=ecommerce&t=parcel';">
              <table>
                <tr>
                  <td><img src="image/btn/add.png"></img></td>
                  <td>{{t escape='html'}}New Parcel{{/t}}</td>
                </tr>
              </table>
            </span>
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td class="header" style="width: 50px;"></td>
    <th style="width: 100px;" onclick="load_parcels('ship_name')">{{t escape='html'}}Shipping{{/t}}</th>
    <!--<th style="width: 100px;" onclick="load_parcels('weight')">{{t escape='html'}}Weight{{/t}}</th>-->
    <th style="width: 200px;" onclick="load_parcels('track_no')">{{t escape='html'}}Track No.{{/t}}</th>
    <th style="width: 150px;">{{t escape='html'}}Delivery Date{{/t}}</th>
    <th style="width: 200px;" onclick="load_parcels('order_no')">{{t escape='html'}}Order No.{{/t}}</th>
    <th style="width: 150px;">{{t escape='html'}}Last Modified{{/t}}</th>
    <th style="width: 150px;">{{t escape='html'}}Added Date{{/t}}</th>
    <td class="header" style="border-right: 0;"></td>
  </tr>
</table>
<div id="{{$t|escape:'html'}}_parcels_pagination" style="margin-top: 15px;"></div>

<script src="jsrequest.php?script={{$t|escape:'html'}}" type="text/javascript"></script>
<script type="text/javascript">
  <!--
    if (document.addEventListener) {
      document.addEventListener('DOMContentLoaded', init, false);
    }
    window.onload = init;
    
    var _result = JSON.parse('{{$result|@json_encode|escape:'javascript'}}');
    var _parcels = _result.d;
    var _move = null;
    var _y = 0;
    var _selects = [];
    
    function init() {
      if (!document.getElementById || !document.createElement || !arguments || arguments.callee._loaded) {
        return;
      }
      arguments.callee._loaded = true;
      
      update_parcels_table();
      
      var select, actions;
      select = document.getElementById('{{$t|escape:'html'}}_parcels_select');
      select.onclick = function() {
        for (var idx in  _selects) {
          var obj = _selects[idx];
          obj.checked = this.checked;
        }
      };
      actions = document.getElementById('{{$t|escape:'html'}}_parcels_actions');
      actions.options[actions.options.length] = new Option('Delete');
      actions.onchange = function() {
        if (this.selectedIndex == 1) {
          var deletes = [];
          for (var idx in  _selects) {
            var obj = _selects[idx];
            if (obj.checked) {
              deletes[deletes.length] = obj._obj;
            }
          }
          new RequestUtils()._custom('delete_parcels', JSON.stringify(deletes), function(result, params) {
            for (var idx in  deletes) {
              var obj = deletes[idx];
              MiscUtils.remove(_parcels, obj, null);
            }
            update_parcels_table();
            actions.selectedIndex = 0;
          }, {
            php: 'ecommerce/custom.php',
            pos : DOMUtils.findPos(this)
          });
        }
      };
      
      new ModulePagination(document, document.getElementById('{{$t|escape:'html'}}_parcels_pagination'), null, null, null, null, {
        page: _result.p,
        total: _result.t,
        limit: _result.s,
        callbackFunc: function(page) { 
          location.href = '?path=ecommerce&t=parcels&o=' + _result.o + '&q=' + _result.q + '&p=' + page;
        },
        contenxt: this
      });
      
      //searchbox
      var search_text = document.getElementById('searchbox_search_text');
      var search_button = document.getElementById('searchbox_search_button');
      search_text.onkeyup = function(e) { var code = DOMUtils.getEventCode(e);
                                          if (code == 13) {
                                            location.href = '?path=ecommerce&t=parcels&o=' + _result.o + '&q=' + _result.q + '&p=' + _result.p + '&con=' + search_text.value;
                                          }
                                      };
      search_button.onclick = function() {
        location.href = '?path=ecommerce&t=parcels&o=' + _result.o + '&q=' + _result.q + '&p=' + _result.p + '&con=' + search_text.value;
      };
    }
    
    function load_parcels(o) {
      var q = _result.q;
      if (o == _result.o && _result.q == 'ASC') {
        q = 'DESC';
      } else if (o == _result.o && _result.q == 'DESC') {
        q = 'ASC';
      }
      location.href = '?path=ecommerce&t=parcels&o=' + o + '&q=' + q + '&p=' + _result.p; 
    }
    
    function update_parcels_table() {
      var table, tr, td, elm, div, p;
      
      table = document.getElementById('{{$t|escape:'html'}}_parcels');
      DOMUtils.removeTableRows(table, 2);
      if (_parcels.length == 0) {
        tr = table.insertRow(-1);
        tr.className = 'nohover';
        td = tr.insertCell(-1);
        td.className = 'border';
        td.colSpan = 8;
        td.style.padding = '5px 0 5px 10px';
        td.style.height = '35px';
        td.appendChild(document.createTextNode('{{t escape='javascript'}}No Parcels Found{{/t}}'));
        
      } else {
        _parcels = MiscUtils.sortByOrder(_parcels);
        
        for (var idx in _parcels) {
          var obj = _parcels[idx];
          
          tr = table.insertRow(-1);
          tr.className = (idx % 2 == 1) ? 'rowodd' : 'roweven';
          
          td = tr.insertCell(-1);
          elm = document.createElement('input');
          elm.type = 'checkbox';
          elm._obj = obj;
          if (obj.is_custom == 0) {
            elm.disabled = true;
          } else {
            elm.disabled = false;
            _selects.push(elm);
          }
          td.appendChild(elm);
          
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode(obj.ship_name));
          
          //td = tr.insertCell(-1);
          //td.appendChild(document.createTextNode(obj.weight));
          
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode(obj.track_no));
          
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode(MiscUtils.formatDate(obj.date_delivery, '{{$lang|escape:'javascript'}}', true)));
          
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode((obj.order_no) ? obj.order_no : ''));
          
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode(MiscUtils.formatDate(obj.date_last_mod, '{{$lang|escape:'javascript'}}')));
          
          td = tr.insertCell(-1);
          td.appendChild(document.createTextNode(MiscUtils.formatDate(obj.date_added, '{{$lang|escape:'javascript'}}')));
          
          td = tr.insertCell(-1);
          elm = document.createElement('img');
          elm.className = '{{$t|escape:'javascript'}}_image_button';
          elm.src = 'image/edit.png';
          elm._obj = obj;
          elm.onclick = function() {
            location.href = 'admin.php?path=ecommerce&t=parcel&i=' + this._obj.id;
          }
          td.appendChild(elm);
        }
      }
    }
  -->
</script>
